package com.wanmait.travel.controller.manager;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanmait.travel.dao.ScenicDAO;
import com.wanmait.travel.dao.ScenicInfoDAO;
import com.wanmait.travel.vo.Scenic;

@WebServlet("/ScenicInfoController")
public class ScenicInfoController extends  HttpServlet 
{
	private ScenicInfoDAO scenicInfoDAO =ScenicInfoDAO.getDAO();
	private ScenicDAO scenicDAO= ScenicDAO.getDAO();
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String action =request.getParameter("action");
		
		if(action==null) {
			response.sendRedirect("/Travel/scenic/scenicInfoShow.jsp");
			return;
		}
		switch(action) {
			case"findScenic":
				this.findScenic(request,response);//�������
				break;
			case"scenicFind":
				this.scenicFind(request,response);
				break;
			case"sreach":
				this.sreach(request, response);
				break;
			case"sreachTL":
				this.sreachTL(request, response);
				break;
			
		}
	}
	
	protected void sreachTL(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
		String id=request.getParameter("id");
		int scenicId=Integer.parseInt(id);
		Scenic scenic=scenicInfoDAO.scenicTL(scenicId);
		response.getWriter().print(scenic.getTen()+","+scenic.getLatitude());
	}

	protected void provinceFindScenic(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		
	}

	protected void scenicFind(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException
	{
		request.setCharacterEncoding("UTF-8");
		String  idstr =request.getParameter("id");
		int id =Integer.parseInt(idstr);
		List<Scenic> scenics = scenicInfoDAO.findByCityId(id);
		request.setAttribute("scenics",scenics);
		request.getRequestDispatcher("/ajax/scenicFind.jsp").forward(request,response);
	}

	protected void findScenic(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException
	{
		request.setCharacterEncoding("UTF-8");
		String  idstr =request.getParameter("id");
		int id =Integer.parseInt(idstr);
		List<Scenic> scenics = scenicInfoDAO.findByCityId(id);
		request.setAttribute("scenics",scenics);
		request.getRequestDispatcher("/scenic/scenicListShow.jsp").forward(request,response);
	}
	
	protected void sreach(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException
	{
	String travel=request.getParameter("travel");
	List<Scenic> scenics=scenicDAO.findByName(travel);
	request.setAttribute("scenics", scenics);
	request.getRequestDispatcher("/ajax/scenicListShow.jsp").forward(request, response);
	
		
		
		
	}
	
	
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		doGet(request, response);
	}
}
